<?php

namespace App\Trip\Train\Order\Impl;

use App\Models\Train\TrainOrderPassengers\TrainOrderPassenger;
use App\Models\Train\TrainOrders\TrainOrder;
use App\Models\Trip\TripOrderRefund\TripOrderRefund;
use App\Trip\Train\Order\Interfaces\TicketReturnInterface;
use App\Trip\Train\Providers\PiaoChi\Order\OrderQuery;
use App\Trip\Train\Providers\PiaoChi\Passenger\OrderPassenger;
use App\Trip\Train\Providers\PiaoChi\TrainOrderService;
use Exception;
use Illuminate\Support\Facades\Log;

class PiaoChiTicketReturnImpl implements TicketReturnInterface
{

    private TrainOrderPassenger $passenger;
    private TrainOrderService   $order_service;
    private TripOrderRefund     $refund_info;
    private TrainOrder          $order_info;

    public function __construct(TrainOrder $order, TrainOrderPassenger $passenger, TripOrderRefund $refund_info)
    {
        $this->order_info    = $order;
        $this->passenger     = $passenger;
        $this->refund_info   = $refund_info;
        $this->order_service = new TrainOrderService();
    }

    /**
     * @throws Exception
     */
    public function ticket_return()
    {
        $order              = $this->order_info;
        $date               = $order->getTrainDate();
        $main_train         = $order->getMainTrain();
        $from_station       = $main_train->getFromName();
        $to_station         = $main_train->getToName();
        $train_code         = $main_train->getTrainCode();
        $piaochi_order      = new OrderQuery($order->getOutOrderId());
        $piaochi_passengers = $piaochi_order->getOriginOrderPassengers();
        $piaochi_passenger  = collect($piaochi_passengers)
            ->where('sub_passenger_id', $this->passenger->subPassengerId())
            ->first();
        $rebuild            = (new OrderPassenger($piaochi_passenger['original']));
        $trip_passengers    = [];
        $trip_passengers[]  = [
            "realname"         => $this->passenger->getTrueName(),
            "realcode"         => $this->passenger->getIdentityCode(),
            "realcode_guoji"   => $rebuild->getOriginGuoJi(),
            "cardtype"         => $rebuild->getOriginCardType(),
            "peopletype"       => $rebuild->getOriginPeopleType(),
            "seattip"          => $rebuild->getOriginSeatTip(),
            "carriagecode"     => $rebuild->getOriginCarriage(),
            "seatcode"         => $rebuild->getOriginSeatCode(),
            "seatcodes"        => $rebuild->getOriginSeatCodes(),
            "card_fileids"     => [
            ],
            "paytype"          => $this->order_service::PAY_TYPE_ELECTRONIC, // 理论上要读取 订单支付方式
            "ticketcode"       => $rebuild->getOriginTicketNumber(),
            "price"            => $rebuild->getOriginPrice(),
            "sub_passenger_id" => $this->passenger->subPassengerId(),
        ];

        $trip = [
            [
                "type"        => $this->order_service::BUSINESS_TYPE_REFUND,
                "flytime"     => $date,
                "fromcity"    => $from_station,
                "tocity"      => $to_station,
                "carcode"     => $train_code,
                "info"        => "",
                "passenger"   => $trip_passengers,
                "sub_trip_id" => $order->getOrderSn(), // 回调用于判别是哪笔订单
            ],
        ];

        $book = $this->order_service->ticketReturn(['trip' => $trip, 'trade_no' => $this->refund_info->getRefundSn()]);
        $res  = json_encode($book, JSON_UNESCAPED_UNICODE);
        Log::info("Train:Ticket:return:{$order->getOrderSn()}，{$this->passenger->subPassengerId()},{$res}");
        if ($book['order_id']) {
            $this->refund_info->saveOutOrderId($book['order_id']);
        }
    }
}